var _beforeUnload_time = 0;

$(function () {
    window.onload = function(){
        Global.initial();    
    }

    window.onbeforeunload = function (event) {
        _beforeUnload_time = new Date().getTime();
        
    };

    window.onunload = function () {
        var _gap_time = new Date().getTime() - _beforeUnload_time;
        if (_gap_time <= 5) $.cookie('entranceOpen', 'close');
    }

    Util.SwapTab(".tabs2", "li", ".tab-content2", ".tab-pane", "active", "click");

//    $("#BtnMoreNewsUl").unbind("click").click(function() {
//        window.location = "/service.html";
//    })

//    $("#BtnMoreAnimalUl").unbind("click").click(function() {
//        window.location = "/RareAnimals.html";
//    })

//    $("#BtnMoreActiceUl").unbind("click").click(function() {
//        window.location = "/education.html";
//    })

//    $("#BtnMoreParkScenery").unbind("click").click(function() {
//        window.location = "/SceneView.html";
//    })

    if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i))) {} else {
        if ($.cookie('entranceOpen') != 'open') {
            var $dom_popup = $("#J_EntrancePopup");
            $dom_popup.dialog({
                autoOpen: false,
                width: '1030',
                height: '769',
                title: "",
                dialogClass: "custom-dialog bg-transparent",
                modal: true,
                resizable: false
            }).dialog("open");

            $dom_popup.find(".J_popupClose").unbind("click").click(function() {
                $dom_popup.dialog("close");
                $.cookie('entranceOpen', 'open');
            })
        }
    }

    //轮播图片
    GetArticleBanner({
        pageSize: 10,
        pageNumber: 1
    }, function(Items) {
        $("#J_IndexBanner .swiper-wrapper").empty();
        $.each(Items, function(i, item) {
            var html = ['<div class="swiper-slide ', i == 0 ? 'rightDownBig' : '', '"><img src="', item.ImgPath, '" />', '<div class="detail">', '<h3 class="title">', item.Title, '</h3>', '<p class="sub-title mt10"><span>', item.SubTitle, '</span></p>', '</div>', '</div>'].join("");
            $("#J_IndexBanner .swiper-wrapper").append(html);

        })

        indexSlide();
        
    })

    GetArticleMoudleInfo(1010); //票价信息
    GetArticleMoudleInfo(110); //营业时间信息
    //    GetArticleMoudleInfo(110, $("#IndexBusinessTime")); //票价信息
    //    GetArticleMoudleInfo(120, $("#IndexActive")); //上班时间

    //园区动态
    GetArticleData({
        ArticleType: 41,
        TopNum: 4
    }, function(Items) {
        $("#IndexNewsUl").empty();
        $.each(Items, function(i, item) {
            var html = ['<li class="item">', '<a href="newsDetail.html?id=', item.ArticleID, '" class="ArticleLink">', '<label class="label-like">', '<strong>', Util.getDay(item.Publictime), '</strong>', '<p>', Util.getFullYear(item.Publictime), '/', Util.getMonth(item.Publictime), '</p>', '</label>', '<p class="tit">', item.Title, '</p>', '<p class="resume mt10">', item.Abstract, '</p>', '</a>', '</li>'].join("");
            $("#IndexNewsUl").append(html);
        })

    })

    //科普活动
    GetArticleData({
        ArticleType: 38,
        TopNum: 4
    }, function(Items) {
        $("#IndexActiceUl").empty();
        $.each(Items, function(i, item) {
            var html = ['<li class="item">', '<a href="educationDetail.html?id=', item.ArticleID, '" class="ArticleLink">', '<label class="label-like">', '<strong>', Util.getDay(item.Publictime), '</strong>', '<p>', Util.getFullYear(item.Publictime), '/', Util.getMonth(item.Publictime), '</p>', '</label>', '<p class="tit">', item.Title, '</p>', '<p class="resume mt10">', item.Abstract, '</p>', '</a>', '</li>'].join("");
            $("#IndexActiceUl").append(html);
        })
    })

    //热门动物
    GetArticleData({
        ArticleType: 29,
        TopNum: 4
    }, function(Items) {
        $("#IndexAnimalUl").empty();
        $.each(Items, function(i, item) {
            //var html = ['<li class="item">', '<a href="AnimalDetail.html?id=', item.ArticleID, '" class="ArticleLink">', '<label class="label-like">', '<strong>', Util.getDay(item.Publictime), '</strong>', '<p>', Util.getFullYear(item.Publictime), '/', Util.getMonth(item.Publictime), '</p>', '</label>', '<p class="tit">', item.Title, '</p>', '<p class="resume mt10">', item.Abstract, '</p>', '</a>', '</li>'].join("");
            var html = ['<li class="item">', '<a href="AnimalDetail.html?id=', item.ArticleID, '" class="ArticleLink">', '<label class="label-like"><img src="', item.ArticleLogoSpecial, '"/></label>', '<p class="tit">', item.Title, '</p>', '<p class="resume mt10">', item.Abstract, '</p>', '</a>', '</li>'].join("");
            $("#IndexAnimalUl").append(html);
        })
    })

    //精品场馆
    GetArticleData({
        ArticleType: 30,
        TopNum: 4
    }, function(Items) {
        getSceneSlide(Items);
    })

    //动物行为展示
    GetArticleData({
        ArticleType: 31,
        TopNum: 2
    }, function(Items) {
        $("#IndexActive").empty();
        $.each(Items, function(i, item) {
            var customData = eval(item.CustomData);
            var subhtml = "";
            if (customData) {
                for (var i = 0; i < customData.length; i++) {
                    var str = '<li><span class="value">' + customData[i].DataValue + '</span>' + customData[i].DataName + '</li>'
                    subhtml += str;
                }
            }
            var html = ['<div class="bg-card item mr20"><div class="pic">',
                '<img src="', get_smallpic(item.ArticleSmallLogo), '" />',
                '<p class="mt10 text-black">',
                item.SubTitle, '</p></div>',
                '<p class="text-black">',
                item.Title,
                '</p>',
                '<ul class="info-list mt10">',
                subhtml,
                '</ul></div>'
            ].join("");
            $("#IndexActive").append(html);
        })

    })

    //园内风光
    GetArticleData({
        ArticleType: 42,
        TopNum: 3
    }, function(Items) {
        $("#SceneDiv").empty();
        $.each(Items, function(i, item) {
            var html = ['<div class="col-sm-6 col-lg-4">', '<div class="item">', '<a href="/SceneView.html?ArticleID=', item.ArticleID, '">', '<p class="pic">', '<img src="', item.ArticleLogoSpecial, '" />', '</p>', '</a>', '</div>', '</div>'].join("");
            $("#SceneDiv").append(html);
            $("#SceneDiv .SceneLink:last").data("data", item);
        })

    })

    //轮播主题乐园图片
    GetArticleData({
        ArticleType: 36,
        TopNum: 5
    }, function (Items) {
        $("#J_ThemeParkSlide .swiper-wrapper").empty();
        $.each(Items, function (i, item) {
            var html = ['<div class="swiper-slide"><img src="', item.ArticleLogoSpecial, '" /></div>'].join("");
            $("#J_ThemeParkSlide .swiper-wrapper").append(html);
        })
    })

    //轮播水上世界图片
    GetArticleData({
        ArticleType: 35,
        TopNum: 5
    }, function (Items) {
        $("#J_WaterWorldSlide .swiper-wrapper").empty();
        $.each(Items, function (i, item) {
            var html = ['<div class="swiper-slide"><img src="', item.ArticleLogoSpecial, '" /></div>'].join("");
            $("#J_WaterWorldSlide .swiper-wrapper").append(html);

        })
    })

    function GetArticleMoudleInfo(ArticleMoudleID, $contain) {
        //生成文章栏目下拉菜单
        Service.post({
            url: "/ArticleInfoService.svc/GetArticleMoudleInfo",
            params: {
                ArticleMoudleID: ArticleMoudleID
            },
            success: function(response) {
                var item = $.parseJSON(response.MoudleData);
                if (ArticleMoudleID == 1010) {
                    $("#ZoomManPrice").html("￥ " + item.ZoomManPrice);
                    $("#ZoomChildrenPrice").html("￥ " + item.ZoomChildrenPrice);
                    $("#ZoomStudentPrice").html("￥ " + item.ZoomStudentPrice);
                    $("#WaterManPrice").html("￥ " + item.WaterManPrice);
                    $("#WaterChildrenPrice").html("￥ " + item.WaterChildrenPrice);
                    $("#WaterStudentPrice").html("￥ " + item.WaterStudentPrice);
                } else if (ArticleMoudleID == 110) {
                    var html = ['<h5>营业时间</h5>',
                            '<div class="mt20">',
                                '<img src="/Images/index/img_home_opennng.png" class="fr" />',
                                '<ul class="info-list w160 fl">',
                                    '<li><span class="value">', item.ZoomOpenStatus == 1 ? (item.ZoomOpenHour + ':' + item.ZoomOpenMinute + ' - ' + item.ZoomCloseHour + ':' + item.ZoomCloseMinute) : '暂未开放', '</span>南宁动物园 </li>',
                                    '<li><span class="value">', item.WaterWorldOpenStatus == 1 ? (item.WaterWorldOpenHour + ':' + item.WaterWorldOpenMinute + ' - ' + item.WaterWorldCloseHour + ':' + item.WaterWorldCloseMinute) : '暂未开放', '</span>加勒比水世界 </li>',
                                '</ul>',
                            '</div>'].join('');
                    $("#IndexBusinessTime").html(html);
                }
                
            }
        });
    }

    function GetArticleBanner(params, callback) {
        //获取首页大图列表
        Service.post({
            url: "/ArticleInfoService.svc/GetArticleWebBannerForPublic",
            params: params,
            success: function (response) {
                if (callback) {
                    callback(response.Items);

                }
            }
        });
    }

    function GetArticleData(params, callback) {
        //生成文章栏目下拉菜单
        Service.post({
            url: "/ArticleInfoService.svc/GetArticleInfoForPublic",
            params: params,
            success: function(response) {
                if (callback) {
                    callback(response.Items);

                }
            }
        });
    }

    ThemeParkSlide();
    WaterWorldSlide();
})

function indexSlide() {
    var swiper = new Swiper('#J_IndexBanner .swiper-container', {
        autoHeight: true,
        autoplay: {
            delay: 3000,
            stopOnLastSlide: false,
            disableOnInteraction: false,
        },
        //effect: 'fade',
        pagination: {
            el: '#J_IndexBanner .swiper-pagination',
            clickable: true
        },
        on: {
            slideChangeTransitionStart: function() {
                nextSlide = swiper.slides.eq(this.activeIndex);
                nextSlide.addClass("centerBig");
            },
            slideChangeTransitionEnd: function() {
                prevSlide = swiper.slides[this.previousIndex];
                prevSlide.className = "swiper-slide";
            }
        }
    });
}


function getSceneSlide(Items) {
    var itemLength = 4;
    var oWidth = $(window).width();
    if (oWidth <= 900) {
        itemLength = 2;
    }
    var html = '',
        deviceListArr = [];
    $deviceList = $("#J_venueSlide .swiper-wrapper");
    $deviceList.empty();
    var i = 0;

    var html = '',
        linkAr = [];


    $.each(Items, function(index, item) {
        var html = ['<div class="col-sm-6 col-lg-3">', '<div class="item">', '<a href="AnimalDetail.html?id=', item.ArticleID, '">', '<p class="pic"><img src="', item.ArticleLogoSpecial, '" /></p>', '<div class="text">', '<p class="name">', item.Title, '</p>', '<p class="description mt20">', item.Abstract.length > 20 ? item.Abstract.substring(0, 18) + '……' : item.Abstract, '</p>', '</div>', '</a>', '</div>', '</div>'].join("");
        linkAr.push(html);

        if (linkAr.length === itemLength) {
            linkAr.unshift('<div class="swiper-slide"><div class="row">');
            linkAr.push('</div></div>');

            $deviceList.append(linkAr.join(''));
            linkAr.length = 0;
        }

        if (Items.length === i + 1) {
            linkAr.unshift('<div class="swiper-slide"><div class="row">');
            linkAr.push('</div></div>');
            $deviceList.append(linkAr.join(''));
        }
    });

    if (Items.length <= itemLength) {
        $deviceList.append(linkAr.join(''));
    } else {
        $("#J_venueSlide .swiper-button-next,#J_venueSlide .swiper-button-prev").show();
        var caseSwiper = new Swiper('#J_venueSlide .swiper-container', {
            pagination: '#J_venueSlide .swiper-pagination',
            paginationClickable: true,
            navigation: {
                nextEl: '#J_venueSlide .swiper-button-next',
                prevEl: '#J_venueSlide .swiper-button-prev',
            }
        })
    }

}

function ThemeParkSlide() {
    var swiper = new Swiper('#J_ThemeParkSlide', {
        navigation: {
            nextEl: '#J_ThemeParkSlide .swiper-button-next',
            prevEl: '#J_ThemeParkSlide .swiper-button-prev',
        },
    });
}

function WaterWorldSlide() {
    var swiper = new Swiper('#J_WaterWorldSlide', {
        navigation: {
            nextEl: '#J_WaterWorldSlide .swiper-button-next',
            prevEl: '#J_WaterWorldSlide .swiper-button-prev',
        },
    });
}

//设置默认封面小图
function get_smallpic(Path) {
    var defaultPath = "/Images/index/img_home_show_1.png";
    if (Path == null || Path == '') {
        return defaultPath;
    }
    return Path;
}
